#include "TEffectPool.h"

HRESULT  TEffectPool::Create( TCHAR* strFileName )
{
	HRESULT hr = E_FAIL;
	DWORD dwShaderIndex = 0;
	if( strFileName == NULL )
		dwShaderIndex = I_Shader.Add(m_strEffectPool.c_str(), 0, 0, true);
	else
		dwShaderIndex = I_Shader.Add(strFileName, 0, 0, true);

	if( dwShaderIndex <= 0 ) 
	{
		MessageBox( 0, m_strEffectPool.c_str(), _T("Load error"), MB_OK );
		return false;
	}
	m_pShader = I_Shader.GetPtr( dwShaderIndex );
	m_pEffectPool	= m_pShader->GetEffectPool();
	
	assert( m_pEffectPool );
    ID3D10Effect* pPoolEffect = m_pEffectPool->AsEffect();

	m_pCbNeverChanges		= pPoolEffect->GetConstantBufferByName("cbNeverChanges");	
	m_pCbChangesEveryFrame	= pPoolEffect->GetConstantBufferByName("cbChangesEveryFrame");	
	m_pCbChangeOnResize		= pPoolEffect->GetConstantBufferByName("cbChangeOnResize");
	
	m_cbNeverChanges.cb_GlobalAmbientLightColor= m_pCbNeverChanges->GetMemberByName("cb_GlobalAmbientLightColor")->AsVector();
	m_cbChangesEveryFrame.cb_matView		= m_pCbChangesEveryFrame->GetMemberByName("cb_matView")->AsMatrix();
	m_cbChangesEveryFrame.cb_fTimer			= m_pCbChangesEveryFrame->GetMemberByName("cb_fTimer")->AsScalar();
	m_cbChangeOnResize.cb_matProj			= m_pCbChangeOnResize->GetMemberByName("cb_matProj")->AsMatrix(); 		
	
	SetNeverChange();	
	return hr;
}
bool TEffectPool::Init()
{
	m_LightAadMaterial.m_vAmbientMaterialColor	= D3DXVECTOR4( 0.1f, 0.1f, 0.1f, 1.0f );	
	m_pmatView		= 0;
	m_fTimer		= 0.0f;
	return true;
}
void TEffectPool::SetMatrix( D3DXMATRIX* pWorld, D3DXMATRIX* pView, D3DXMATRIX* pProj )
{
	m_pmatWorld	= pWorld;	
	m_pmatView	= pView;
	m_pmatProj	= pProj;	
}
void TEffectPool::SetChangeOnResize()
{
	if( m_cbChangeOnResize.cb_matProj )
		m_cbChangeOnResize.cb_matProj->SetMatrix((float*)m_pmatProj);
}
void TEffectPool::SetNeverChange()
{
	if( m_cbNeverChanges.cb_GlobalAmbientLightColor)
		m_cbNeverChanges.cb_GlobalAmbientLightColor->SetFloatVector( (float*)&m_LightAadMaterial.m_vAmbientMaterialColor );
}
void TEffectPool::Update(	float fTimer )
{
	m_fTimer	=	fTimer;
	m_cbChangesEveryFrame.cb_matView->SetMatrix((float*)m_pmatView);
	m_cbChangesEveryFrame.cb_fTimer->SetFloat( m_fTimer);
}
TEffectPool::TEffectPool(void)
{
	m_pShader				=	NULL;
	m_pEffectPool			=	NULL;		
	m_pCbNeverChanges		=	NULL;
	m_pCbChangesEveryFrame	=	NULL;
	m_pCbChangeOnResize		=	NULL;
	m_strEffectPool			= L"../../include/Inc_3/TEffectPool.fxh";
	Init();
}

TEffectPool::~TEffectPool(void)
{
}
